package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.dto.SetmealDto;
import com.itheima.pojo.Category;
import com.itheima.pojo.Setmeal;
import com.itheima.service.CategoryService;
import com.itheima.service.SetmealService;
import com.itheima.common.R;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: test
 * @Create by: JJ菜菜
 * @Date: 2021/11/4 14:55
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private CategoryService categoryService;

    /**
     * 套餐管理分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @RequestMapping("/page")
    public R<IPage<Setmeal>> page(Integer page, Integer pageSize, String name){
        IPage<Setmeal> pageInfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasLength(name), Setmeal::getName, name);
        IPage<Setmeal> result = setmealService.page(pageInfo, queryWrapper);
        // 查询套餐分类
        List<Setmeal> records = result.getRecords();

        List<Setmeal> collect = records.stream().map((record) -> {
            // 分类id
            Long categoryId = record.getCategoryId();
            Category category = categoryService.getById(categoryId);
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(record, setmealDto);
            setmealDto.setCategoryName(category.getName());
            return setmealDto;
        }).collect(Collectors.toList());
        result.setRecords(collect);
        return R.success(result);
    }

    /**
     * 添加套餐
     * @param setmealDto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){
        setmealService.saveSetmeal(setmealDto);
        return R.success("添加成功！");
    }

    /**
     *  要求：如果setmeal中有启售就禁止删除，
     *       如果没有启售的话，就删除setmeal中对应的数据，
     *       再删除setmealDish表中关联的数据
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        setmealService.deleteById(ids);
        return R.success("删除成功！");
    }

    /**
     * 修改状态，批量修改
     * @param st
     * @param ids
     * @return
     */
    @PostMapping("/status/{st}")
    public R<String> updateStatus(@PathVariable int st, @RequestParam List<Long> ids){
        for (Long id : ids) {
            // 封装setmeal进行修改
            Setmeal setmeal = new Setmeal();
            setmeal.setId(id);
            setmeal.setStatus(st);
            // 设置条件构建器
            LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Setmeal::getId, id);
            setmealService.update(setmeal, queryWrapper);
        }
        return R.success("修改成功！");
    }

    /*
     * 修改数据回显
     * @param id
     * @return
     */
   @GetMapping("/{id}")
    public R<SetmealDto> selectByupdate(@PathVariable Long id){
        Setmeal setmeal = setmealService.getById(id);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal, setmealDto);
        return R.success(setmealDto);
    }


     // 用户选取套餐下的具体套餐

    @GetMapping("/list")
    public R list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId() != null, Setmeal::getCategoryId, setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus() != null, Setmeal::getStatus, setmeal.getStatus());
        List<Setmeal> setmeals = setmealService.list(queryWrapper);
        return R.success(setmeals);
    }



}
